package com.jzg.jcptadmin.presenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;

import com.jzg.jcptadmin.base.BasePresenter;
import com.jzg.jcptadmin.data.vo.ImageItem;
import com.jzg.jcptadmin.data.vo.ImageList;
import com.jzg.jcptadmin.viewinterface.AddCarActivityInterface;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by jzg on 2015/12/29.
 */
public class AddCarPresenter extends BasePresenter<AddCarActivityInterface> {

    private ImageList imageList;
    private Context con;
    private Bitmap bitmap;
    private AddCarActivityInterface mView;

    public AddCarPresenter(Context con) {
//        this.con = con;
//        imageList = (ImageList) ACache.get(con).getAsObject("imagecache1");
//        if(imageList==null){
//            imageList = new ImageList();
//        }
//        lists = imageList.getLists();
    }
    @Override
    public void attachView(AddCarActivityInterface mvpView) {
        super.attachView(mvpView);
        this.mView = mvpView;
    }

    /**
     * 从相机或者图库添加照片
     * @param uri
     */
    public void addPic(Uri uri) {
        try {
            bitmap = BitmapFactory.decodeStream(con.getContentResolver().openInputStream(uri));

            Observable.create(new Observable.OnSubscribe<ImageItem>() {
                @Override
                public void call(Subscriber<? super ImageItem> subscriber) {

//                    Date date = new Date();
//                    SimpleDateFormat format = new SimpleDateFormat(
//                            "yyyyMMddHHmmssSSS"); // 格式化时间
//                    String dataTake = format.format(date);
//                    File dirFile = new File(Environment.getExternalStorageDirectory().getPath()
//                            + "/JZGErShouChe/photo_temp");
//                    if (!dirFile.exists()) {
//                        dirFile.mkdirs();
//                    }
//                    File dirFileTub = new File(Environment.getExternalStorageDirectory().getPath()
//                            + "/JZGErShouChe/photo_tub");
//                    if (!dirFileTub.exists()) {
//                        dirFileTub.mkdirs();
//                    }
//                    String savePath = dirFile.getPath() + File.separator + dataTake + ".jpg";
//                    String saveTubPath = dirFileTub.getPath() + File.separator + dataTake + ".jpg";
//                    saveImage(bitmap, savePath);
//                    saveImage(ImgUtils.getImageThumbnail(savePath, ScreenUtils.dip2px(con, 110), ScreenUtils.dip2px(con, 110)), saveTubPath);
                    ImageItem item = new ImageItem();
//                    item.setImagePath(savePath);
//                    item.setImageTub(saveTubPath);
////                    item.setImageTub();
//                    lists.add(item);
//                    imageList.setLists((ArrayList<ImageItem>) lists);
//                    ACache.get(con).put("imagecache1", imageList);
                    subscriber.onNext(item);
                    //subscribeOn(): 指定 subscribe() 所发生的线程，即 Observable.OnSubscribe 被激活时所处的线程。或者叫做事件产生的线程。
                }
            }).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())//observeOn(): 指定 Subscriber 所运行在的线程。或者叫做事件消费的线程。
                    .subscribe(new Subscriber<ImageItem>() {
                        @Override
                        public void onCompleted() {

                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(ImageItem s) {
                            getMvpView().addCarBackByPhoto(s);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean saveImage(Bitmap photo, String spath) {
        try {
            BufferedOutputStream bos = new BufferedOutputStream(
                    new FileOutputStream(spath, false));
            photo.compress(Bitmap.CompressFormat.JPEG, 40, bos);
            bos.flush();
            bos.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

}
